﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;

namespace AmmoTracker
{
    public partial class TabControl : UserControl
    {
        private Dictionary<ushort, XIPlugin.AmmoData> ammoTracking;
        private Dictionary<int, XIPlugin.ItemData> itemDict;

        public TabControl(ref Dictionary<ushort, XIPlugin.AmmoData> ammoTr, ref Dictionary<int, XIPlugin.ItemData> items)
        {
            ammoTracking = ammoTr;
            itemDict = items;
            InitializeComponent();
        }

        private void TabControl_Load(object sender, EventArgs e)
        {
            LoadAmmoList();

            if (ammoTrackingGrid.Rows.Count > 0)
            {
                DataGridViewCellEventArgs temp = new DataGridViewCellEventArgs(0, 0);
                ammoTrackingGrid_CellClick(null, temp);
                SaveAmmoTracking();
            }
        }

        private void SetAllJobs(bool checkState)
        {
            ammoJobALL.Checked = checkState;
            ammoJobWAR.Checked = checkState;
            ammoJobDRK.Checked = checkState;
            ammoJobDRG.Checked = checkState;
            ammoJobSAM.Checked = checkState;
            ammoJobTHF.Checked = checkState;
            ammoJobMNK.Checked = checkState;
            ammoJobNIN.Checked = checkState;
            ammoJobPLD.Checked = checkState;
            ammoJobPUP.Checked = checkState;
            ammoJobDNC.Checked = checkState;
            ammoJobRNG.Checked = checkState;
            ammoJobCOR.Checked = checkState;
            ammoJobBLU.Checked = checkState;
            ammoJobBST.Checked = checkState;
            ammoJobWHM.Checked = checkState;
            ammoJobSCH.Checked = checkState;
            ammoJobSMN.Checked = checkState;
            ammoJobRDM.Checked = checkState;
            ammoJobBLM.Checked = checkState;
            ammoJobBRD.Checked = checkState;
        }

        private void ammoTrackingGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            itemsComboBox.Text =ammoTrackingGrid.Rows[e.RowIndex].Cells[1].Value.ToString();
            itemAbreviation.Text = ammoTrackingGrid.Rows[e.RowIndex].Cells[2].Value.ToString();
            SetAllJobs(ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("ALL"));
            ammoJobWAR.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("WAR");
            ammoJobDRK.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("DRK");
            ammoJobDRG.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("DRG");
            ammoJobSAM.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("SAM");
            ammoJobTHF.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("THF");
            ammoJobMNK.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("MNK");
            ammoJobNIN.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("NIN");
            ammoJobPLD.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("PLD");
            ammoJobPUP.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("PUP");
            ammoJobDNC.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("DNC");
            ammoJobRNG.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("RNG");
            ammoJobCOR.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("COR");
            ammoJobBLU.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("BLU");
            ammoJobBST.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("BST");
            ammoJobWHM.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("WHM");
            ammoJobSCH.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("SCH");
            ammoJobSMN.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("SMN");
            ammoJobRDM.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("RDM");
            ammoJobBLM.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("BLM");
            ammoJobBRD.Checked = ammoTrackingGrid.Rows[e.RowIndex].Cells[3].Value.ToString().Contains("BRD");
        }

        private void ammoJobALL_Click(object sender, EventArgs e)
        {
            SetAllJobs(ammoJobALL.Checked);
        }

        private string ammoTrackingJobConversion()
        {
            string jobsString = "";
            if (ammoJobWAR.Checked)
                jobsString += "WAR,";
            if (ammoJobDRK.Checked)
                jobsString += "DRK,";
            if (ammoJobDRG.Checked)
                jobsString += "DRG,";
            if (ammoJobSAM.Checked)
                jobsString += "SAM,";
            if (ammoJobTHF.Checked)
                jobsString += "THF,";
            if (ammoJobMNK.Checked)
                jobsString += "MNK,";
            if (ammoJobNIN.Checked)
                jobsString += "NIN,";
            if (ammoJobPLD.Checked)
                jobsString += "PLD,";
            if (ammoJobPUP.Checked)
                jobsString += "PUP,";
            if (ammoJobDNC.Checked)
                jobsString += "DNC,";
            if (ammoJobRNG.Checked)
                jobsString += "RNG,";
            if (ammoJobCOR.Checked)
                jobsString += "COR,";
            if (ammoJobBLU.Checked)
                jobsString += "BLU,";
            if (ammoJobBST.Checked)
                jobsString += "BST,";
            if (ammoJobWHM.Checked)
                jobsString += "WHM,";
            if (ammoJobSCH.Checked)
                jobsString += "SCH,";
            if (ammoJobSMN.Checked)
                jobsString += "SMN,";
            if (ammoJobRDM.Checked)
                jobsString += "RDM,";
            if (ammoJobBLM.Checked)
                jobsString += "BLM,";
            if (ammoJobBRD.Checked)
                jobsString += "BRD,";
            jobsString = jobsString.TrimEnd(',');
            return jobsString;
        }

        private void SaveAmmoTracking()
        {
            ammoTracking.Clear();

            for (int i = 0; i < ammoTrackingGrid.Rows.Count; i++)
            {
                XIPlugin.AmmoData itemData = new XIPlugin.AmmoData();
                itemData.abreviation = ammoTrackingGrid.Rows[i].Cells[2].Value.ToString();
                itemData.jobs = ammoTrackingGrid.Rows[i].Cells[3].Value.ToString();
                ammoTracking.Add(Convert.ToUInt16(ammoTrackingGrid.Rows[i].Cells[0].Value.ToString()), itemData);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            KeyValuePair<int, string> valPair = (KeyValuePair<int, string>)itemsComboBox.Items[itemsComboBox.SelectedIndex];
            int temp = ammoTrackingGrid.SelectedRows[0].Index;
            if (valPair.Key.ToString() != "")
            {
                ammoTrackingGrid.Rows[temp].Cells[0].Value = valPair.Key.ToString();
                ammoTrackingGrid.Rows[temp].Cells[1].Value = valPair.Value.ToString();
                ammoTrackingGrid.Rows[temp].Cells[2].Value = itemAbreviation.Text;
                if (ammoJobALL.Checked)
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = "ALL";
                else
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = ammoTrackingJobConversion();

                SaveAmmoTracking();
            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            KeyValuePair<int, string> valPair = (KeyValuePair<int, string>)itemsComboBox.Items[itemsComboBox.SelectedIndex];
            if (valPair.Key.ToString() != "")
            {
                int temp = ammoTrackingGrid.Rows.Add(1);
                ammoTrackingGrid.Rows[temp].Cells[0].Value = valPair.Key.ToString();
                ammoTrackingGrid.Rows[temp].Cells[1].Value = valPair.Value.ToString();
                ammoTrackingGrid.Rows[temp].Cells[2].Value = itemAbreviation.Text;
                if (ammoJobALL.Checked)
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = "ALL";
                else
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = ammoTrackingJobConversion();
                if (ammoTrackingGrid.Rows[temp].Cells[3].Value.ToString() == "")
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = "ALL";

                SaveAmmoTracking();

                ammoTrackingGrid.Rows[ammoTrackingGrid.Rows.Count - 1].Selected = true;
            }
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            if(ammoTrackingGrid.SelectedRows.Count > 0)
                ammoTrackingGrid.Rows.RemoveAt(ammoTrackingGrid.SelectedRows[0].Index);

            SaveAmmoTracking();

            if (ammoTrackingGrid.Rows.Count > 0)
            {
                DataGridViewCellEventArgs temp = new DataGridViewCellEventArgs(0, ammoTrackingGrid.SelectedRows[0].Index);
                ammoTrackingGrid_CellClick(null, temp);
            }
            else
            {
                itemsComboBox.Text = "";
                itemAbreviation.Text = "";
                SetAllJobs(false);
            }
        }

        public void SaveAmmoList()
        {
            XmlTextWriter textWriter = new XmlTextWriter("AmmoTracker.xml", null);
            textWriter.WriteStartDocument();
            textWriter.WriteStartElement("AmmoTracker");
            foreach (KeyValuePair<ushort, XIPlugin.AmmoData> item in ammoTracking)
            {
                textWriter.WriteStartElement("item");
                textWriter.WriteAttributeString("id", item.Key.ToString());
                textWriter.WriteAttributeString("abr", item.Value.abreviation);
                textWriter.WriteAttributeString("jobs", item.Value.jobs);
                textWriter.WriteEndElement();
            }
            textWriter.WriteEndElement();
            textWriter.WriteEndDocument();
            textWriter.Close();
        }

        public void LoadAmmoList()
        {
            XmlTextReader textReader = new XmlTextReader("AmmoTracker.xml");
            textReader.Read();
            while (textReader.Read())
            {
                textReader.MoveToElement();
                if (textReader.Name == "item")
                {
                    int temp = ammoTrackingGrid.Rows.Add(1);
                    int id = Convert.ToInt32(textReader["id"]);
                    ammoTrackingGrid.Rows[temp].Cells[0].Value = id;
                    if (itemDict.ContainsKey(id))
                        ammoTrackingGrid.Rows[temp].Cells[1].Value = itemDict[id].name;
                    else
                        ammoTrackingGrid.Rows[temp].Cells[1].Value = "Invalid ID: " + id.ToString();
                    ammoTrackingGrid.Rows[temp].Cells[2].Value = textReader["abr"];
                    ammoTrackingGrid.Rows[temp].Cells[3].Value = textReader["jobs"];
                }
            }
        }
    }
}
